package com.along.htybrowser.adapter;

import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.along.htybrowser.R;
import com.along.htybrowser.bean.ReloadUrlBean;
import com.along.htybrowser.databinding.FavoriteRowBinding;
import com.along.htybrowser.greendao.tab.FavoriteTab;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏适配器
 * RecyclerView添加ContextMenu的两种方案详解
 * https://blog.csdn.net/MakerCloud/article/details/82756355
 * @author wen66to88@163.com
 * @date 2021/7/9 - 15:22
 */
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {
    private final List<FavoriteTab> beanList;
    private int position;

    public int getContextMenuPosition() {
        return position;
    }

    public void setContextMenuPosition(int position) {
        this.position = position;
    }

    public FavoriteAdapter(List<FavoriteTab> beanList) {
        this.beanList = beanList;
    }

    @NonNull
    @Override
    public FavoriteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FavoriteRowBinding binding = FavoriteRowBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull FavoriteAdapter.ViewHolder holder, int position) {
        FavoriteTab bean = beanList.get(position);
        if (bean.getWebsite().startsWith("https://")) {
            holder.binding.imageViewFavicon.setImageResource(android.R.drawable.ic_secure);
        } else if (bean.getWebsite().startsWith("http://")) {
            holder.binding.imageViewFavicon.setImageResource(android.R.drawable.ic_partial_secure);
        } else if (bean.getWebsite().startsWith("file://")) {
            holder.binding.imageViewFavicon.setImageResource(android.R.drawable.stat_notify_sdcard);
        } else {
            holder.binding.imageViewFavicon.setImageResource(R.drawable.network);
        }
        holder.binding.website.setText(bean.getWebsite());
        holder.binding.title.setText(bean.getTitle());
        holder.binding.getRoot().setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(holder.binding.getRoot(), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList == null ? 0 : beanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener{
        FavoriteRowBinding binding;

        public ViewHolder(FavoriteRowBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.getRoot().setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            //注意传入的menuInfo为null
//            ModelUser mSelectModelUser = get.get(getContextMenuPosition());
//            Log.i("UserAdapter", "onCreateContextMenu: "+getContextMenuPosition());
//            menu.setHeaderTitle(mSelectModelUser.getUserName());
//            ((UserActivity)mContext).CreateMenu(menu);
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


}